package cn.ymjr.business.controller;

import cn.ymjr.business.entity.SysUserAddress;
import cn.ymjr.business.form.ExplainForm;
import cn.ymjr.business.form.OrderForm;
import cn.ymjr.business.form.StartForm;
import cn.ymjr.business.service.BusinessService;
import cn.ymjr.sys.config.exception.BusinessException;
import cn.ymjr.sys.util.DictInit;
import cn.ymjr.sys.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author Tiny
 * @create 2022/4/1 17:53
 */
@CrossOrigin("*")
@RestController
@RequestMapping("business")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @RequestMapping(value = "explain",method = RequestMethod.POST)
    public Result explain(@RequestBody ExplainForm explainForm,@RequestHeader HttpHeaders headers){
        String token = headers.getFirst("token");
        try {
            return businessService.nameExplain(explainForm,token);
        }catch (BusinessException e){
            return new Result(e.getMsg());
        }
    }
    @RequestMapping(value = "activityList",method = RequestMethod.GET)
    public Result activityList(Integer page,Integer pageSize){
        return businessService.activityList(page, pageSize);
    }
    @RequestMapping(value = "activityDetail",method = RequestMethod.GET)
    public Result activityDetail(Integer id){
        return businessService.activityDetail(id);
    }

    @RequestMapping(value = "categoryList",method = RequestMethod.GET)
    public Result categoryList(){
        //Map<String, String> category = DictInit.constantMap.get("goods_category");
        return businessService.goodsCategory();
    }
    @RequestMapping(value = "goodsList",method = RequestMethod.GET)
    public Result goodsList(Integer page,Integer pageSize,Integer category){
        return businessService.goodsList(page, pageSize, category);
    }
    @RequestMapping(value = "goodsDetail",method = RequestMethod.GET)
    public Result goodsDetail(Integer id){
        return businessService.goodsDetail(id);
    }

    @RequestMapping(value = "infoList",method = RequestMethod.GET)
    public Result infoList(Integer page,Integer pageSize,Integer userId,Integer category){
        return businessService.informationList(page, pageSize,userId,category);
    }
    @RequestMapping(value = "infoDetail",method = RequestMethod.GET)
    public Result infoDetail(Integer id){
        return businessService.infoDetail(id);
    }

    @RequestMapping(value = "infoStart",method = RequestMethod.POST)
    public Result infoStart(@RequestBody StartForm startForm,@RequestHeader HttpHeaders headers){
        String token = headers.getFirst("token");
        try {
            return businessService.infoStar(startForm,token);
        }catch (BusinessException e){
            return new Result(e.getMsg());
        }
    }
    @RequestMapping(value = "myInfo",method = RequestMethod.GET)
    public Result myInfo(Integer page,Integer pageSize,Integer userId,@RequestHeader HttpHeaders headers){
        String token = headers.getFirst("token");
        try {
            return businessService.myInfo(page,pageSize,userId,token);
        }catch (BusinessException e){
            return new Result(e.getMsg());
        }
    }
    @RequestMapping(value = "editAddress",method = RequestMethod.POST)
    public Result editAddress(@RequestBody SysUserAddress sysUserAddress,@RequestHeader HttpHeaders headers){
        String token = headers.getFirst("token");
        try {
            return businessService.editAddress(sysUserAddress,token);
        }catch (BusinessException e){
            return new Result(e.getMsg());
        }
    }
    @RequestMapping(value = "addressList",method = RequestMethod.GET)
    public Result addressList(Integer userId,Integer page,Integer pageSize,@RequestHeader HttpHeaders headers){
        String token = headers.getFirst("token");
        try {
            return businessService.addressList(userId,page,pageSize,token);
        }catch (BusinessException e){
            return new Result(e.getMsg());
        }
    }
    @RequestMapping(value = "submitOrder",method = RequestMethod.POST)
    public Result submitOrder(@RequestBody OrderForm orderForm,@RequestHeader HttpHeaders headers){
        String token = headers.getFirst("token");
        try {
            return businessService.submitOrder(orderForm,token);
        }catch (BusinessException e){
            return new Result(e.getMsg());
        }
    }
    @RequestMapping(value = "orderList",method = RequestMethod.GET)
    public Result orderList(Integer userId,Integer page,Integer pageSize,Integer orderStatus){
        return businessService.orderList(userId,page,pageSize,orderStatus);
    }

    @RequestMapping(value = "orderPay",method = RequestMethod.GET)
    public Result orderPay(Integer orderId, HttpServletRequest request,@RequestHeader HttpHeaders headers){
        String token = headers.getFirst("token");
        try {
            return businessService.orderPay(orderId,token,request);
        }catch (BusinessException e){
            return new Result(e.getMsg());
        }
    }
    @RequestMapping(value = "cancel",method = RequestMethod.GET)
    public Result cancel(Integer userId,Integer orderId,@RequestHeader HttpHeaders headers){
        String token = headers.getFirst("token");
        try {
            return businessService.cancel(orderId,userId,token);
        }catch (BusinessException e){
            return new Result(e.getMsg());
        }
    }
    @RequestMapping(value = "ensure",method = RequestMethod.GET)
    public Result ensure(Integer userId,Integer orderId,@RequestHeader HttpHeaders headers){
        String token = headers.getFirst("token");
        try {
            return businessService.ensureOrder(userId,orderId,token);
        }catch (BusinessException e){
            return new Result(e.getMsg());
        }
    }
    @RequestMapping(value = "callback")
    public String callback(HttpServletRequest request, HttpServletResponse response){
        try {
            return businessService.callBack(request, response);
        } catch (Exception e) {
            String xml="<xml>"
                    +"<return_code>FAIL</return_code>"
                    +"<return_msg>支付通知失败</return_msg>"
                    +"</xml>";
            return xml;
        }
    }
}
